package org.jeecg.modules.solitaire_app.app.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.solitaire_app.app.entity.YsyUsers;
import org.jeecg.modules.solitaire_app.app.entity.YsyWalletLog;
import org.jeecg.modules.solitaire_app.app.mapper.YsyUsersMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyWalletLogService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 资金记录表
 * @Author: jeecg-boot
 * @Date: 2023-09-07
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "资金记录表")
@RestController
@RequestMapping("/app/ysyWalletLog")
public class YsyWalletLogController extends JeecgController<YsyWalletLog, IYsyWalletLogService> {
    @Autowired
    private IYsyWalletLogService ysyWalletLogService;
    @Autowired
    private YsyUsersMapper ysyUsersMapper;

    /**
     * 分页列表查询
     *
     * @param ysyWalletLog
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "资金记录表-分页列表查询")
    @ApiOperation(value = "资金记录表-分页列表查询", notes = "资金记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsyWalletLog ysyWalletLog,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        if (ysyWalletLog.getType() == 0) {
            ysyWalletLog.setType(null);
        }
        QueryWrapper<YsyWalletLog> queryWrapper = QueryGenerator.initQueryWrapper(ysyWalletLog, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<YsyWalletLog> page = new Page<YsyWalletLog>(pageNo, pageSize);
        IPage<YsyWalletLog> pageList = ysyWalletLogService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param ysyWalletLog
     * @return
     */
    @AutoLog(value = "资金记录表-添加")
    @ApiOperation(value = "资金记录表-添加", notes = "资金记录表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsyWalletLog ysyWalletLog) {
        ysyWalletLogService.save(ysyWalletLog);
        return Result.ok("添加成功！");
    }

    /**
     * 我的佣金页面
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "资金记录表-我的佣金页面")
    @ApiOperation(value = "资金记录表-我的佣金页面", notes = "资金记录表-我的佣金页面")
    @GetMapping(value = "/getMyBalance/{userId}")
    public Result<JSONObject> getMyBalance(@PathVariable("userId") String userId) {
        Result<JSONObject> result = new Result<>();
        YsyUsers ysyUsers = ysyUsersMapper.selectOne(Wrappers.<YsyUsers>lambdaQuery()
                .eq(YsyUsers::getId, userId));
        if (ObjectUtil.isEmpty(ysyUsers)){
            result.setCode(400);
            result.setMessage("未找到该用户");
            result.setSuccess(false);
            return result;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("balance", ysyUsers.getBalance());

        //已结算
        List<YsyWalletLog> ysyWalletLogs = ysyWalletLogService.list(Wrappers.<YsyWalletLog>lambdaQuery()
                .eq(YsyWalletLog::getUserId, userId)
                .eq(YsyWalletLog::getStatus, 1));
        BigDecimal totalYjs = BigDecimal.ZERO;
        for (int i = 0;i<ysyWalletLogs.size();i++){
            totalYjs = totalYjs.add(ysyWalletLogs.get(i).getMoney());
        }
        jsonObject.put("totalYjs", totalYjs);

        //未结算
        List<YsyWalletLog> ysyWalletLogList = ysyWalletLogService.list(Wrappers.<YsyWalletLog>lambdaQuery()
                .eq(YsyWalletLog::getUserId, userId)
                .eq(YsyWalletLog::getStatus, 0));
        BigDecimal totalWjs = BigDecimal.ZERO;
        for (int i = 0;i<ysyWalletLogList.size();i++){
            totalWjs = totalWjs.add(ysyWalletLogList.get(i).getMoney());
        }
        jsonObject.put("totalWjs", totalWjs);

        //驳回金额
        List<YsyWalletLog> ysyWalletLog = ysyWalletLogService.list(Wrappers.<YsyWalletLog>lambdaQuery()
                .eq(YsyWalletLog::getUserId, userId)
                .eq(YsyWalletLog::getStatus, 2));
        BigDecimal totalBhje = BigDecimal.ZERO;
        ysyWalletLog.stream().forEach(item -> {
            totalBhje.add(item.getMoney());
        });
        jsonObject.put("totalBhje", totalBhje);
        result.setResult(jsonObject);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }

    /**
     * 编辑
     *
     * @param ysyWalletLog
     * @return
     */
    @AutoLog(value = "资金记录表-编辑")
    @ApiOperation(value = "资金记录表-编辑", notes = "资金记录表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsyWalletLog ysyWalletLog) {
        ysyWalletLogService.updateById(ysyWalletLog);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资金记录表-通过id删除")
    @ApiOperation(value = "资金记录表-通过id删除", notes = "资金记录表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ysyWalletLogService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "资金记录表-批量删除")
    @ApiOperation(value = "资金记录表-批量删除", notes = "资金记录表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ysyWalletLogService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "资金记录表-通过id查询")
    @ApiOperation(value = "资金记录表-通过id查询", notes = "资金记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YsyWalletLog ysyWalletLog = ysyWalletLogService.getById(id);
        return Result.ok(ysyWalletLog);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysyWalletLog
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsyWalletLog ysyWalletLog) {
        return super.exportXls(request, ysyWalletLog, YsyWalletLog.class, "资金记录表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsyWalletLog.class);
    }

}
